package my.leetcode.solution;

import java.util.HashMap;
import java.util.Map;

public class P3 {
    public static void main(String[] args) {
//        System.out.println(lengthOfLongestSubstring("abbbbbcdefda"));
//        System.out.println(lengthOfLongestSubstring("abcdefg"));
        System.out.println(lengthOfLongestSubstring("pwwkew"));
    }

    public static int lengthOfLongestSubstring(String s) {
        int len = s.length();
        if(len < 2) {
            return s.length();
        }
        Map<Character, Integer> map = new HashMap<>();
        int result = 0, start = -1, nowCount = 0;
        for(int i = 0;i < len;i++) {
            char c = s.charAt(i);
            Integer index = map.get(c);
            if(index != null && index > start) {
                result = Math.max(result, nowCount);
                nowCount = i - index;
                start = index;
            } else {
                nowCount++;
            }
            map.put(c, i);
        }
        return Math.max(result, nowCount);
    }
}
